package com.baselibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.R;
import com.baselibrary.utils.SpacesRightDec;
import com.liaoinstan.springview.utils.DensityUtil;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/11/17 23:42
 */
public class PagerIndicatorView extends RecyclerView {

    private PagerIndicatorAdapter mIndicatorAdapter;
    private int mSelectedRes, mUnSelectedRes;

    public PagerIndicatorView(@NonNull Context context) {
        super(context);
    }

    public PagerIndicatorView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PagerIndicatorView);
        mSelectedRes = typedArray.getResourceId(R.styleable.PagerIndicatorView_selected_indicator, R.drawable.shape_pager_indicator_owned);
        mUnSelectedRes = typedArray.getResourceId(R.styleable.PagerIndicatorView_up_selected_indicator, R.drawable.shape_pager_indicator_abandoned);
        typedArray.recycle();
        initView();
    }


    private void initView() {


        setLayoutManager(new LinearLayoutManager(getContext(), HORIZONTAL, false));
        addItemDecoration(new SpacesRightDec(DensityUtil.dip2px(getContext(), 6)));
        mIndicatorAdapter = new PagerIndicatorAdapter();
        mIndicatorAdapter.setInflater(LayoutInflater.from(getContext()));
        setAdapter(mIndicatorAdapter);
    }

    public void setSumCount(int sumCount) {
        if (mIndicatorAdapter != null)
            mIndicatorAdapter.setSumCount(sumCount);
    }

    public void setCurrentShowView(int indexOf) {
        if (mIndicatorAdapter != null)
            mIndicatorAdapter.setCurrentShowView(indexOf);
    }

    public int getCount() {
        if (mIndicatorAdapter != null)
            return mIndicatorAdapter.getCount();
        return 0;
    }


    class PagerIndicatorAdapter extends Adapter<PagerIndicatorAdapter.PagerIndicatorHolder> {
        private LayoutInflater mInflater;
        private int mShowCount;
        private int mCurrentShowView;

        public void setInflater(LayoutInflater mInflater) {
            this.mInflater = mInflater;
        }

        public void setSumCount(int mShowCount) {
            this.mShowCount = mShowCount;
            notifyDataSetChanged();
        }

        public void setCurrentShowView(int mCurrentShowView) {
            this.mCurrentShowView = mCurrentShowView;
            notifyDataSetChanged();
        }

        public int getCount() {
            return mShowCount;
        }

        @NonNull
        @Override
        public PagerIndicatorHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new PagerIndicatorHolder(mInflater.inflate(R.layout.item_pager_lndicator, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull PagerIndicatorHolder pagerIndicatorHolder, int pos) {
            if (mCurrentShowView == pos) {
                pagerIndicatorHolder.mViewIndicatorAbandoned.setVisibility(GONE);
                pagerIndicatorHolder.mViewIndicatorOwned.setVisibility(VISIBLE);
            } else {
                pagerIndicatorHolder.mViewIndicatorAbandoned.setVisibility(VISIBLE);
                pagerIndicatorHolder.mViewIndicatorOwned.setVisibility(GONE);
            }
        }

        @Override
        public int getItemCount() {
            return mShowCount;
        }


        public class PagerIndicatorHolder extends ViewHolder {
            private View mViewIndicatorAbandoned;
            private View mViewIndicatorOwned;

            public PagerIndicatorHolder(@NonNull View itemView) {
                super(itemView);
                mViewIndicatorAbandoned = itemView.findViewById(R.id.view_indicator_abandoned);
                mViewIndicatorOwned = itemView.findViewById(R.id.view_indicator_owned);

                mViewIndicatorOwned.setBackgroundResource(mSelectedRes);
                mViewIndicatorAbandoned.setBackgroundResource(mUnSelectedRes);
            }
        }
    }


}
